package uk.co.g4me.frameworks.spring.roo.addons.shiro;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.model.JavaPackage;
import org.springframework.roo.project.maven.Pom;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;

/**
 * Sample of a command class. The command class is registered by the Roo shell following an
 * automatic classpath scan. You can provide simple user presentation-related logic in this
 * class. You can return any objects from each method, or use the logger directly if you'd
 * like to emit messages of different severity (and therefore different colours on 
 * non-Windows systems).
 * 
 * @since 1.1
 */
@Component // Use these Apache Felix annotations to register your commands class in the Roo container
@Service
public class ShiroCommands implements CommandMarker { // All command types must implement the CommandMarker interface
    
    /**
     * Get a reference to the ShiroOperations from the underlying OSGi container
     */
    @Reference private ShiroOperations operations;
    
    /**
     * This method allows automatic command hiding in situations when the command should not be visible.
     * For example the 'entity' command will not be made available before the user has defined his persistence settings 
     * in the Roo shell or directly in the project.
     * 
     * You can define multiple methods annotated with {@link CliAvailabilityIndicator} if your commands have differing
     * visibility requirements.
     * 
     * @return true (default) if the command should be visible at this stage, false otherwise
     */
    @CliAvailabilityIndicator({ "shiro setup"})
    public boolean isSetupCommandAvailable() {
        return operations.isSetupCommandAvailable();
    }
    
    /**
     * This method registers a command with the Roo shell. It has no command attribute.
     * 
     */
    @CliCommand(value = "shiro setup", help = "Setup Shiro security, module names are mandatory for persistence and web layers. Package names default to ~.domain and ~.web but are configurable.")
    public void setup(
    		@CliOption(key = "entityModule", mandatory = true, help = "The module which will persist User, Role and Permission entities") final Pom entityModule,
    		@CliOption(key = "webModule", mandatory = true, help = "The module which contains your web layer.") final Pom webModule,
    		@CliOption(key = "entityPackage", mandatory = false, help = "The package in which to store User, Role and Permission entities.", unspecifiedDefaultValue = "~.domain") final String entityPackage,
			@CliOption(key = "controllerPackage", mandatory = false, help = "The package in which to store User, Role and Permission controllers.", unspecifiedDefaultValue = "~.web") final String controllerPackage) {
        operations.setup(entityModule, webModule, entityPackage, controllerPackage);
    }
}